<script setup lang="ts">
import HomePanel from './home-panel.vue'
import useStore from '@/store';
// import { ref } from 'vue';
import { useLazyData } from '@/utils/hooks';
import HomeSkeleton from './home-skeleton.vue';
const { home } = useStore()
// import { useIntersectionObserver } from '@vueuse/core';
// home.getNewList()
// 自己封装一个 hook 业内人士的命名规范: useXXX
// const target = ref(null)
// const { stop } = useIntersectionObserver(target, ([{ isIntersecting }]) => {
//   // 从进入可视区到离开会触发
//   // 从离开可视区到进入也会触发
//   console.log('进入新鲜好物了:', isIntersecting)
//   if (isIntersecting) {
//     home.getNewList()
//     // 停止监视, 以后不会再触发
//     stop()
//   }
// })

// 原始封装
// useLazyData(target, () => home.getNewList())

// 借助引用数据类型的特性实现封装
// const target = useLazyData(() => home.getNewList())

// 极致精简 => 将 api 函数直接作为回调传入, 自动执行
const target = useLazyData(home.getNewList)
</script>

<template>
  <HomePanel ref="target" title="新鲜好物" subTitle="新鲜出炉 品质靠谱">
      <template #right>
        <XtxMore></XtxMore>
      </template>
       <transition name="fade">
          <ul class="goods-list" v-if="home.newList.length">
        <li v-for="item in home.newList" :key="item.id">
          <RouterLink to="/">
            <img
              v-lazy="item.picture"
              alt=""
            />
            <p class="name ellipsis">{{item.name}}</p>
            <p class="price">&yen;{{item.price}}</p>
          </RouterLink>
        </li>
      </ul>
      <HomeSkeleton :count="4" v-else></HomeSkeleton>
       </transition>
    </HomePanel>
</template>

<style scoped lang="less">
.goods-list {
  display: flex;
  justify-content: space-between;
  height: 406px;
  li {
    width: 306px;
    height: 406px;
    background: #f0f9f4;
    .hoverShadow();
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding: 12px 30px 0 30px;
      text-align: center;
    }
    .price {
      color: @priceColor;
    }
  }
}
</style>